﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main.Model
{
    public class Word
    {

        private static List<WordInfo> words = new List<WordInfo>();

        static Word()
        {
            if (File.Exists(SystemConfig.WordFilePath))
            {
                string[] strs = File.ReadAllLines(SystemConfig.WordFilePath);
                foreach (var str in strs)
                {
                    WordInfo wordInfo = JsonConvert.DeserializeObject<WordInfo>(str);
                    words.Add(wordInfo);
                }
            }
        }
        public static void AddNewWord(WordInfo word)
        {
            if (!Exist(word.EnglishName))
            {
                string str1 = JsonConvert.SerializeObject(word);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(SystemConfig.WordFilePath, true))
                {
                    file.WriteLine(str1);
                }
                words.Add(word);
            }
        }
        public static void AddNewWords(List<WordInfo> words)
        {
            foreach (var item in words)
            {
                AddNewWord(item);
            }
        }
        public static bool Exist(string englishName)
        {
            if (words.Exists(delegate(WordInfo word) { return word.EnglishName == englishName; }))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static List<WordInfo> GetWords()
        {
            return words;
        }
    }
    /// <summary>
    /// 英语单词
    /// </summary>
    public class WordInfo
    {
        public WordInfo(string englishName, string chineseName, 年级 grade)
        {
            this.englishName = englishName;
            this.chineseName = chineseName;
            this.grade = grade;
        }
        private string englishName;

        public string EnglishName
        {
            get { return englishName; }
            set { englishName = value; }
        }
        private string chineseName;
        public string ChineseName
        {
            get { return chineseName; }
            set { chineseName = value; }
        }
        private 年级 grade = 年级.二;
        public 年级 Grade
        {
            get
            {
                return this.grade;
            }
            set
            {
                this.grade = value;
            }
        }


    }


}
